<?php

class Permission extends Auth_Controller {
	function __construct(){
		parent::Controller();
		$this->myauth->execute_auth ();
		$this -> load -> model("acl/PermissionModel","",true) ;
		$this->load->model('acl/ModuleModel','',true);
	}
	/**
	 * 权限分类页面
	 */
	function index ()
	{
		parse_str($_SERVER['QUERY_STRING'],$_GET);
		$mid = $this-> input -> get("mid" , true ) ;//获取分类模块ID
		$data['modules'] = $this->ModuleModel->get_module_mid($mid);
		$data['list'] = $this->PermissionModel->get_permission_byMid($mid);
		$data['mid'] = $mid;
		$this->load->view('acl/permission_inc',$data);
	}
	/**
	 * 权限添加页面
	 */
	function add ()
	{
		parse_str($_SERVER['QUERY_STRING'],$_GET);
		$data['mid'] = $this-> input -> get("mid" , true ) ;//获取分类模块ID
		$data['action'] = "acl/permission/doadd";
		$this->load->view('acl/permission_add',$data);
	}
	/**
	 * 执行新添权限
	 */
	function doadd () {
		$data['permission']['mid'] = $this-> input -> post("mid" , true ) ;//获取分类模块ID
		$data['modules'] = $this->ModuleModel->get_module_mid($data['permission']['mid']);
		$data['permission']['name']  = $this-> input -> post("name" , true ) ;
		$data['permission']['permission']  = $this-> input -> post("permission" , true ) ;
		$data['permission']['sortid'] = $this->input->post('sortid');
		$data['mid'] = $data['permission']['mid'];
		$this->form_validation->set_rules($this->setPermissionRules());
		if ($this->form_validation->run() == FALSE){
			$this->load->view('acl/permission_inc',$data);
		}else{
			if($this->PermissionModel->exists_permission($data['permission']['name'],$data['permission']['mid'],null)){
				echo "exits" ;
			}else{
				$pid = $this->PermissionModel->add_permission($data['permission']);
				if($pid > 0){
					$data['list'] = $this->PermissionModel->get_permission_byMid($data['permission']['mid']);
					$this->load->view('acl/permission_inc',$data);
				}else{
					echo "falid";
				}
			}
		}
	}
	/**
	 * 验证新添权限
	 */
	function setPermissionRules()
	{
		return array(
		array(
				"label"=>"权限名称",
				"field"=>"name",
				"rules"=>"required"
				),array(
				"label"=>"路径",
				"field"=>"permission",
				"rules"=>"required"
				),array(
				"label"=>"排序",
				"field"=>"sortid",
				"rules"=>"required|numeric"
				)
				);
	}
	/**
	 * 修改权限
	 */
	function edit () {
		parse_str($_SERVER['QUERY_STRING'],$_GET);
		$data['mid'] = $this-> input -> get("mid" , true ) ;//获取分类模块ID
		$pid = $this-> input -> get("pid" , true ) ;//权限ID
		$data['permission'] = $this->PermissionModel->get_permission_byPid($pid);
		$data['action'] = "acl/permission/doedit";
		$this->load->view('acl/permission_add',$data);


	}
	/**
	 * 执行修改权限
	 */
	function doedit (){
		$data['permission']['mid'] = $this-> input -> post("mid" , true ) ;//获取分类模块ID
		$data['modules'] = $this->ModuleModel->get_module_mid($data['permission']['mid']);
		$data['mid'] = $data['permission']['mid'];
		$data['permission']['name']  = $this-> input -> post("name" , true ) ;
		$data['permission']['permission']  = $this-> input -> post("permission" , true ) ;
		$data['permission']['sortid'] = $this->input->post('sortid' , true );
		$data['permission']['pid'] = $this->input->post('pid' , true );
		$this->form_validation->set_rules($this->setPermissionRules());
		if ($this->form_validation->run() == FALSE){
			$this->load->view('acl/permission_inc',$data);
		}else{
			if($this->PermissionModel->exists_permission($data['permission']['name'],$data['permission']['mid'],$data['permission']['pid'])){
				echo "exits" ;
			}else{
				$flag = $this->PermissionModel->edit_permission($data['permission']);
				if($flag){
					$data['list'] = $this->PermissionModel->get_permission_byMid($data['permission']['mid']);
					$this->load->view('acl/permission_inc',$data);
				}else{
					echo "falid";
				}
			}
		}
	}
	/**
	 * 权限删除
	 */
	function dodelete ()
	{
		parse_str($_SERVER['QUERY_STRING'],$_GET);
		$data['mid'] = $this-> input -> get("mid" , true ) ;//获取分类模块ID
		$data['modules'] = $this->ModuleModel->get_module_mid($data['mid']);
		$pid = $this-> input -> get("pid" , true ) ;//权限ID
		$flag = $this->PermissionModel->delete_permission($pid);
		if($flag){
			$data['list'] = $this->PermissionModel->get_permission_byMid($data['mid']);
			$this->load->view('acl/permission_inc',$data);
		}else {
			echo "faild";
		}
	}
}
?>
